package com.myrobot.ui;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JWindow;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

import com.myrobot.ui.ToolsWindow;

class ScreenShotWindow extends JWindow
{
  private int orgx;
  private int orgy;
  private int endx;
  private int endy;
  private BufferedImage image = null;
  private BufferedImage tempImage = null;
  private BufferedImage saveImage = null;
  
  private ToolsWindow tools = null;
  
  public ScreenShotWindow() throws AWTException
  {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0, 0, d.width, d.height);
    

    Robot robot = new Robot();
    this.image = robot.createScreenCapture(new Rectangle(0, 0, d.width, d.height));
    
    addMouseListener(new MouseAdapter()
    {
      public void mousePressed(MouseEvent e)
      {
       orgx = e.getX();
       orgy = e.getY();
        
        if (ScreenShotWindow.this.tools != null) {
         tools.setVisible(false);
        }
      }
      

      public void mouseReleased(MouseEvent e)
      {
        if (ScreenShotWindow.this.tools == null) {
         tools = new ToolsWindow(ScreenShotWindow.this, e.getX(), e.getY());
        } else {
         tools.setLocation(e.getX(), e.getY());
        }
       tools.setVisible(true);
       tools.toFront();
      }
      
    });
    addMouseMotionListener(new MouseMotionAdapter()
    {

      public void mouseDragged(MouseEvent e)
      {
       endx = e.getX();
       endy = e.getY();
        

        Image tempImage2 =createImage(ScreenShotWindow.this.getWidth(),getHeight());
        Graphics g = tempImage2.getGraphics();
        g.drawImage(ScreenShotWindow.this.tempImage, 0, 0, null);
        int x = Math.min(ScreenShotWindow.this.orgx,endx);
        int y = Math.min(ScreenShotWindow.this.orgy,endy);
        int width = Math.abs(ScreenShotWindow.this.endx -orgx) + 1;
        int height = Math.abs(ScreenShotWindow.this.endy -orgy) + 1;
        
        g.setColor(Color.BLUE);
        g.drawRect(x - 1, y - 1, width + 1, height + 1);
        
       saveImage =image.getSubimage(x, y, width, height);
        g.drawImage(ScreenShotWindow.this.saveImage, x, y, null);
        
       getGraphics().drawImage(tempImage2, 0, 0, ScreenShotWindow.this);
      }
    });
  }
  
  public void paint(Graphics g)
  {
    RescaleOp ro = new RescaleOp(0.8F, 0.0F, null);
    this.tempImage = ro.filter(this.image, null);
    g.drawImage(this.tempImage, 0, 0, this);
  }
  
  public void saveImage() throws IOException
  {
    JFileChooser jfc = new JFileChooser();
    jfc.setDialogTitle("保存");
    

    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", new String[] { "jpg" });
    jfc.setFileFilter(filter);
    

    SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
    String fileName = sdf.format(new Date());
    File filePath = FileSystemView.getFileSystemView().getHomeDirectory();
    File defaultFile = new File(filePath + File.separator + fileName + ".jpg");
    jfc.setSelectedFile(defaultFile);
    
    int flag = jfc.showSaveDialog(this);
    if (flag == 0) {
      File file = jfc.getSelectedFile();
      String path = file.getPath();
      
      if ((!path.endsWith(".jpg")) && (!path.endsWith(".JPG"))) {
        path = path + ".jpg";
      }
      
      ImageIO.write(this.saveImage, "jpg", new File(path));
      System.exit(0);
    }
  }
}